package com.monkeybrother.thread.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * @Author MonkeyBrother
 * @Date 2021/1/21 22:16
 * @Desc
 **/
public class NotSafeThread {
    private static Integer num = 100;
    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(1);
        IntStream.range(1, 20).forEach(v -> new Thread(() -> {
            try {
                latch.await();
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            num--;
            System.out.println(Thread.currentThread().getName() + " 获取到的num值为：" +  num);
        }).start());
        latch.countDown();
    }
}
